package com.example.win.android_banduyujia.peopleactivity.ui.bitmap;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;

import com.example.win.android_banduyujia.peopleactivity.activity.activity.openactivity.TestPicActivity;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.ref.SoftReference;
import java.util.HashMap;

/**
 * Created by Win on 2015/9/10.
 */
public class BitmapCache extends Activity{
    public final String TAG = getClass().getSimpleName();

    public Handler handler = new Handler();
    public HashMap<String, SoftReference<Bitmap>> imageCache = new HashMap<String, SoftReference<Bitmap>>();

    private void put(String path, Bitmap bmp) {
        if (!TextUtils.isEmpty(path) && bmp != null) {
            imageCache.put(path, new SoftReference<Bitmap>(bmp));
        }
    }

    public void displayBmp(final ImageView iv, final String thumbPath, final String sourcePath, final ImageCallback callback) {
        if(TextUtils.isEmpty(thumbPath) && TextUtils.isEmpty(sourcePath)){
            Log.e(TAG, "no paths pass in");
            return;
        }
        final String  path;
        final boolean isThumbPath;
        if(!TextUtils.isEmpty(thumbPath)){
            path = thumbPath;
            isThumbPath =true;
        }else if(!TextUtils.isEmpty(sourcePath)){
            path =sourcePath;
            isThumbPath = false;
        }else{
            return;
        }
         if(imageCache.containsKey(path)){
             SoftReference<Bitmap>  reference = imageCache.get(path);
             Bitmap  bmp = reference.get();
             if(bmp !=null){
                 if(callback !=null){
                     callback.imageLoad(iv, bmp, sourcePath);
                 }
                 iv.setImageBitmap(bmp);
                 Log.d(TAG, "hit cache");
                 return;
             }
         }
        iv.setImageBitmap(null);

        new Thread(){
            Bitmap  thumb;

            @Override
            public void run() {
                try{
                    if(isThumbPath){
                        thumb = BitmapFactory.decodeFile(thumbPath);
                        if(thumb == null){
                            thumb = revitionImageSize(sourcePath);
                        }
                    }else{
                        thumb = revitionImageSize(sourcePath);
                    }
                }catch (Exception e){

                }
                if(thumb == null){
                    thumb = TestPicActivity.bimap;
                }
                Log.e(TAG, "-------thumb------" + thumb);
                put(path, thumb);

                if(callback != null){
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            callback.imageLoad(iv, thumb, sourcePath);
                        }
                    });
                }
            }
        }.start();
    }

    private Bitmap revitionImageSize(String path) throws IOException {
        BufferedInputStream  bis;
        bis = new BufferedInputStream(new FileInputStream(new File(path)));

        BitmapFactory.Options  options=new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(bis, null, options);

        bis.close();
        int i = 0;
        Bitmap  bitmap;
        while (true){
            if((options.outWidth >> i <= 256) && (options.outHeight >> i <= 256)){
                bis = new BufferedInputStream(new FileInputStream(new File(path)));

                options.inSampleSize= (int) Math.pow(2.0D,i);
                options.inJustDecodeBounds = false;
                bitmap =BitmapFactory.decodeStream(bis, null, options);
                break;
            }
            i += 1;
        }
        return bitmap;
    }

    public interface ImageCallback {
        public void imageLoad(ImageView imageView, Bitmap bitmap,
                              Object... params);
    }
}
